<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;
use app\wechat\controller\Platform;
use think\response\Json;
use think\facade\Cookie;
use app\wechat\controller\Openid;
use app\admin\model\Authorizer;
use app\admin\model\Fansrepetition;

class Fans extends Base
{
    public $serv;
    public function _initialize()
    {
        parent::_initialize();
    }
    public function repeatRate()
    {
        return $this->fetch('fans/repeatRate');
    }
    public function duplicateCheck()
    {
        return $this->fetch('fans/duplicateCheck');
    }
    public function getRepetList()
    {
        $key = $this->request->post('keyword');
        $page = $this->request->post('page');
        $size = $this->request->post('size');
        $list = Db::table('nx_fans_repetition')
            ->where('ghid_1|ghid_2|name_1|name_2','like','%'.$key.'%')
            ->where('delstatus',1)
            ->page($page,$size)
            ->order('createtime desc')
            ->select();
        $pagecount =  Db::table('nx_fans_repetition')
            ->where('ghid_1|ghid_2|name_1|name_2','like','%'.$key.'%')
            ->where('delstatus',1)
            ->count();
        foreach ($list as $k=>$v){
            switch ($v['status'])
            {
                case 1:
                    $list[$k]['statusname'] = "<span style='color: #40AFFE'>未执行</span>";
                    break;
                case 2:
                    $list[$k]['statusname'] = "<span style='color: #0073b7;'>执行中</span>";
                    break;
                case 3:
                    $list[$k]['statusname'] = "<span style='color: #18bc9c;'>执行完毕</span>";
                    break;
                case 4:
                    $list[$k]['statusname'] =  "<span style='color: red;'>执行失败</span>";
                    break;
                default:
                    $list[$k]['statusname'] = "<span style='color: red;'>未知状态</span>";
                    break;
            }
        }
        return $this->api_resp_success('',array("count"=>$pagecount),$list);
    }
    public function delFansRepet()
    {
        $ids = $this->request->post('id');
        $res = Db::table('nx_fans_repetition')->where('id','in',$ids)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('','',$res);
        }else{
            return $this->api_resp_error('');
        }
    }
    public function getFansInfo($token)
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$token;
        $list = $this->http_get($url);
        if(!$list){
            return $this->api_resp_error('请求失败');
        }
        $data = json_decode($list,true);
        $nextOpenid = $data['next_openid'];
        $openidList = $data['data']['openid'];
        if($data['total'] > 10000){
            $num = ceil($data['total']/10000);
            for ($i = 0;$i<$num-1;$i++){
                $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$token.'&next_openid='.$nextOpenid;
                $list = $this->http_get($url);
                $data = json_decode($list,true);
                $nextOpenid = $data['next_openid'];
                if ($data['data']['openid']) {
                    $openidList = array_merge($openidList,$data['data']['openid']);
                }
            }
        }
        $dataList['fanstotal'] = $data['total'];
        $dataList['openidList'] = $openidList;
        return $dataList;
    }
    //获取个人的信息
    public function getUnionid($ghid,$openid)
    {
        $token = Cookie::get('access_token'.$ghid);
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$token.'&openid='.$openid.'&lang=zh_CN';
        $info = $this->http_get($url);
        $fansinfo = json_decode($info,true);
        return $fansinfo;
    }
    /**
     * 粉丝对此重复----手动输入;
     * $info1 公众号1的粉丝信息；
     * $info2公众号2的粉丝信息；
     **/
    public function contrastInfo()
    {
        $ghid1 = $this->request->post('ghid1');
        $ghid2 = $this->request->post('ghid2');

        $acc1 = Authorizer::where('ghid',$ghid1)->field('appid,name')->find();
        $acc2 = Authorizer::where('ghid',$ghid2)->field('appid,name')->find();
        $openid    = new Openid();
        $repetnum = count($openid->get_fans_intersection($acc1['appid'],$acc2['appid']));
        $total1 = $openid->get_fans_info_count($acc1['appid']);
        $total2 = $openid->get_fans_info_count($acc2['appid']);
        if($total1 == 0 && $total2 == 0){
            return $this->api_resp_error('粉丝数都为0');
        }
        $repetrate = $repetnum/($total1+$total2-$repetnum);
        $data = [
            'ghid_1'=>$ghid1,
            'name_1'=>$acc1['name'],
            'fanstotal1'=>$total1,
            'ghid_2'=>$ghid2,
            'name_2'=>$acc2['name'],
            'fanstotal2'=>$total2,
            'repet_fans_total'=>$repetnum,
            'repetrate'=>(round($repetrate,4) * 100) .'%',
            'status'=>3,
            'createtime'=>date('Y-m-d H:i:s',time()),
        ];
        $res = Db::name('nx_fans_repetition')->insert($data);
        if($res){
            return $this->api_resp_success('执行完成','',$res);
        }
    }
    /**
     * 粉丝对此重复----数据库单条查重
     * $info1 公众号1的粉丝信息；
     * $info2公众号2的粉丝信息；
     **/
    public function contrastInfo2()
    {
        $ghid1 = $this->request->post('ghid1');
        $ghid2 = $this->request->post('ghid2');

        $acc1 = Authorizer::where('ghid',$ghid1)->field('appid,name')->find();
        $acc2 = Authorizer::where('ghid',$ghid2)->field('appid,name')->find();
        $openid    = new Openid();
        $repetnum = count($openid->get_fans_intersection($acc1['appid'],$acc2['appid']));
        $total1 = $openid->get_fans_info_count($acc1['appid']);
        $total2 = $openid->get_fans_info_count($acc2['appid']);
        if($total1 == 0 && $total2 == 0){
            return $this->api_resp_error('粉丝数都为0');
        }
        $repetrate = $repetnum/($total1+$total2-$repetnum);
        $data = [
            'fanstotal1'=>$total1,
            'fanstotal2'=>$total2,
            'repet_fans_total'=>$repetnum,
            'repetrate'=>(round($repetrate,4) * 100) .'%',
            'status'=>3,
            'updatetime'=>date('Y-m-d H:i:s',time()),
        ];
        $res = Fansrepetition::where(['ghid_1' => $ghid1, 'ghid_2' => $ghid2])->update($data);
        if($res){
            return $this->api_resp_success('执行完成','',$res);
        }
    }
    /**
     * 粉丝对此重复----数据库全部查重
     * $info1 公众号1的粉丝信息；
     * $info2公众号2的粉丝信息；
     **/
    public function contrastInfo3()
    {
        $accountlist = Fansrepetition::where(['delstatus'=>1,'status'=>1])
            ->field('id,ghid_1,ghid_2')
            ->select();
        $otheraccount = Fansrepetition::where('fanstotal1=0 OR fanstotal2 =0')->select();
        if(count($accountlist) == 0 && count($otheraccount) == 0){
            return $this->api_resp_error('没有可执行对比');
        }
        foreach ($otheraccount as $ke => $val){
            $acc3 = Authorizer::where('ghid',$val['ghid_1'])->field('appid,name')->find();
            $acc4 = Authorizer::where('ghid',$val['ghid_2'])->field('appid,name')->find();
            $openid    = new Openid();
            $repetnum2 = count($openid->get_fans_intersection($acc3['appid'],$acc4['appid']));
            $total3 = $openid->get_fans_info_count($acc3['appid']);
            $total4 = $openid->get_fans_info_count($acc4['appid']);
            if($total3 == 0 && $total4 == 0){
                $res = Fansrepetition::where('id',$val['id'])->update(['status'=>4,'errormsg'=>'粉丝数都为0','updatetime'=>date('Y-m-d H:i:s',time())]);
            }else{
                $repetrate2 = $repetnum2/($total3+$total4-$repetnum2);
                $udata = [
                    'fanstotal1'=>$total3,
                    'fanstotal2'=>$total4,
                    'repet_fans_total'=>$repetnum2,
                    'repetrate'=>(round($repetrate2,4) * 100) .'%',
                    'status'=>3,
                    'updatetime'=>date('Y-m-d H:i:s',time()),
                ];
                $res = Fansrepetition::where('id',$val['id'])->update($udata);
            }
        }
        foreach ($accountlist as $k=>$v){
            $acc1 = Authorizer::where('ghid',$v['ghid_1'])->field('appid,name')->find();
            $acc2 = Authorizer::where('ghid',$v['ghid_2'])->field('appid,name')->find();
            $openid    = new Openid();
            $repetnum = count($openid->get_fans_intersection($acc1['appid'],$acc2['appid']));
            $total1 = $openid->get_fans_info_count($acc1['appid']);
            $total2 = $openid->get_fans_info_count($acc2['appid']);
            if($total1 == 0 && $total2 == 0){
                $res = Fansrepetition::where('id',$v['id'])->update(['status'=>4,'errormsg'=>'粉丝数都为0','updatetime'=>date('Y-m-d H:i:s',time())]);
            }else{
                $repetrate = $repetnum/($total1+$total2-$repetnum);
                $data = [
                    'fanstotal1'=>$total1,
                    'fanstotal2'=>$total2,
                    'repet_fans_total'=>$repetnum,
                    'repetrate'=>(round($repetrate,4) * 100) .'%',
                    'status'=>3,
                    'updatetime'=>date('Y-m-d H:i:s',time()),
                ];
                $res = Fansrepetition::where('id',$v['id'])->update($data);
            }
        }
        return $this->api_resp_success('执行完成','',$res);
    }
}
